package com.zcsy.manage.web.mall;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.constants.OwnerConstants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.CommonImg;
import com.zcsy.manage.model.base.ShopBanner;
import com.zcsy.manage.model.base.ShopCategory;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.mall.AdCategory;
import com.zcsy.manage.model.mall.AdInfo;
import com.zcsy.manage.model.mall.CouponInfo;
import com.zcsy.manage.model.mall.GoodsCategory;
import com.zcsy.manage.model.mall.GoodsComment;
import com.zcsy.manage.model.mall.GoodsInfo;
import com.zcsy.manage.model.mall.GoodsRead;
import com.zcsy.manage.model.mall.GoodsSpecification;
import com.zcsy.manage.model.mall.GoodsSpecificationAttr;
import com.zcsy.manage.model.mall.IntegralCategory;
import com.zcsy.manage.model.mall.vo.GoodsAttrVo;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.weixin.StringUtil;
import com.zcsy.manage.service.base.ICommonImgService;
import com.zcsy.manage.service.base.IShopBannerService;
import com.zcsy.manage.service.base.IShopCategoryService;
import com.zcsy.manage.service.base.IShopInfoService;
import com.zcsy.manage.service.common.CommonService;
import com.zcsy.manage.service.mall.IAdCategoryService;
import com.zcsy.manage.service.mall.IAdInfoService;
import com.zcsy.manage.service.mall.ICouponInfoService;
import com.zcsy.manage.service.mall.IGoodsCategoryService;
import com.zcsy.manage.service.mall.IGoodsCommentService;
import com.zcsy.manage.service.mall.IGoodsInfoService;
import com.zcsy.manage.service.mall.IGoodsReadService;
import com.zcsy.manage.service.mall.IGoodsSpecificationService;
import com.zcsy.manage.service.mall.IIntegralCategoryService;

/**
 * @Title: MallController.java
 * @Package： com.zcsy.manage.web.mall 
 * @Description: 商城控制层
 *
 * @author caiyinli 13277030520@163.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年7月29日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
public class MallController {
	
	@Autowired
	private IAdCategoryService adCategoryService;
	
	@Autowired
	private IAdInfoService adInfoService;
	
	@Autowired
	private IGoodsInfoService goodsInfoService;
	
	@Autowired
	private IGoodsReadService goodsReadService;
	
	@Autowired
	private IIntegralCategoryService integralCategoryService;
	
	@Autowired
	private IShopInfoService shopInfoService;
	
	@Autowired
	private IShopCategoryService shopCategoryService;
	
	@Autowired
	private IGoodsCategoryService goodsCategoryService;
	
	@Autowired
	private IGoodsCommentService goodsCommentService;
	
	@Autowired
	private ICouponInfoService couponInfoService;
	
	@Autowired
	private IGoodsSpecificationService goodsSpecService;
	
	@Autowired
	private ICommonImgService commonImgService;
	
	@Autowired
    private IShopBannerService shopBannerService;
    
	public static final int PAGE_SIZE  = 12; //每页大小

    /**
     * @Description 返回秒杀商品页面<br>
     * 
     * @param model
     * @param pNo 当前页码
     * @param order 排序：1、默认2、销量从低到高 3、销量从高到低4、价格从低到高5、价格从高到低
     * @param secType 秒杀商品类型：1：正在进行 2、即将开始
     * @param req 用户请求
     * @return 返回秒杀商品页面
     * @author caiyinli 13277030520@163.com
     * @date 2016年8月3日
     * @see 
     */
    @RequestMapping(value = "mallSeckill", method = RequestMethod.GET)
    public String mallSeckill(Model model, Integer pNo,Integer order,Integer secType,HttpServletRequest req){
    	
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
         
    	if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
    	
    	if (order == null) {
    		order = OwnerConstants.INIT_ORDER;
        }
    	
    	if (secType == null) {
    		secType = OwnerConstants.SECKILL_ON_GOING;
    	}
    	
    	//此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, PAGE_SIZE);
        List<GoodsInfo> list = goodsInfoService.getGoodsInfoByFilter(order,secType);
        int saleCount;
        for (GoodsInfo goodsInfo : list) {
        	saleCount = goodsInfo.getStock()+goodsInfo.getSales();
        	if(saleCount != 0){
        		goodsInfo.setSaleRate(goodsInfo.getSales() * 100 / saleCount);
        	}
		}
        
        PageInfo<GoodsInfo> info = new PageInfo<GoodsInfo>(list);
    	
    	/*//秒杀专区广告位
		AdCategory adSeckill = adCategoryService.selectByValue("seckill");
		if (adSeckill != null) {
    		List<AdInfo> adSeckills = adInfoService.selectByAdCategoryId(adSeckill.getId(),cellId);
    		model.addAttribute("adSeckills", adSeckills);
		}*/
		
		model.addAttribute("seckillGoods", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("order", order);
		model.addAttribute("secType", secType);
        
        return "mall/mall_seckill";
    		
    }
    
    /**
     * @Description 返回店铺列表页面<br>
     * 
     * @param model
     * @param pNo 当前页面 
     * @param shopCategoryId 店铺分类id
     * @param req 用户请求
     * @return  返回店铺列表页面
     * @author caiyinli 13277030520@163.com
     * @date 2016年8月3日
     * @see 
     */
    @RequestMapping(value = "hotShopList", method = RequestMethod.GET)
    public String hotShopList(Model model,Integer pNo,String shopCategoryId, HttpServletRequest req){
    	
        String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
    	if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
    	//此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, PAGE_SIZE);
        List<ShopInfo> shopInfos = shopInfoService.getListByCellIdAndCategoryId(cellId, shopCategoryId);
        PageInfo<ShopInfo> info = new PageInfo<ShopInfo>(shopInfos);
        
        List<ShopCategory> shopCategories = shopCategoryService.getShopCategoryByCellId(cellId);
        
        //热门店铺
  		AdCategory hotShop = adCategoryService.selectByValue("hotShops");
  		if (hotShop != null) {
      		List<AdInfo> adHotShopInfos = adInfoService.selectByAdCategoryId(hotShop.getId(),cellId);
      		model.addAttribute("adHotShopInfos", adHotShopInfos);
  		}
        
        model.addAttribute("shopInfoList", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("shopCategoryList", shopCategories);
		model.addAttribute("shopCategoryId", shopCategoryId);
		
    	return "mall/hot_shop";
    	
    }
    
    /**
     * @Description 返回积分商品列表页面<br>
     * @param pNo 当前页
     * @param order 1：默认排序 2：销量从低到高 3：销量从高到低 4：兑换积分从低到高 5：兑换积分从高到低
     * @author lilan 1063972142@qq.com
     * @date 2016年8月3日
     */
    @RequestMapping(value = "mallIntegral", method = RequestMethod.GET)
    public String mallIntegral(Model model, Integer pNo,Integer order,Integer min, Integer max, 
    		Integer findIntegral,HttpServletRequest req){
    	
    	if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
    	
    	if (order == null) {
    		order = OwnerConstants.INIT_ORDER;
        }
    	
    	if (findIntegral == null) {
    		findIntegral = OwnerConstants.INIT_ORDER;
        }
    	if (min == null) {
    		min = OwnerConstants.INIT_ORDER;
        }
    	if (max == null) {
    		max = OwnerConstants.INIT_ORDER;
        }
    	
    	//此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, PAGE_SIZE);
        List<GoodsInfo> list = goodsInfoService.getIntegralGoodsByFilter(order,findIntegral,min,max);
        
        PageInfo<GoodsInfo> info = new PageInfo<GoodsInfo>(list);
    	
    	//获取积分分类信息
		List<IntegralCategory> integralCategories = integralCategoryService.selectAll();
		
		 // 获取当前用户
        User user = CommonService.getSessionUser();
		
		model.addAttribute("integralCategories", integralCategories);
		model.addAttribute("user", user);
		model.addAttribute("integralGoods", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("order", order);
        
        return "mall/integralgoods/mall_integral";
    		
    }
    
    /**
     * @Description 返回商品详情页面<br> 
     * @param id 主键id
     * @param type 1:正常商品，2：积分商品，3：秒杀商品
     * @author lilan 1063972142@qq.com
     * @date 2016年8月3日
     * @return
     */
    @RequestMapping(value = "GoodsDetail", method = RequestMethod.GET)
    public String integralGoodsDetail(Model model,String id, Integer type, String attrId, HttpServletRequest req){
    	GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(id);
    	ShopInfo shopInfo = shopInfoService.selectByPrimaryKey(goodsInfo.getShopId());
    	
    	// 获取当前用户
        User user = CommonService.getSessionUser();
        GoodsRead goodsRead = new GoodsRead();
        goodsRead.setId(UniqueNumberUtils.get18UniqueNumber());
        goodsRead.setGoodsId(id);
        goodsRead.setUserAccount(user.getLoginname());
        goodsRead.setSendTime(new Date());
        goodsReadService.insert(goodsRead);
        
        
        // 默认选择中一个规格
        GoodsSpecification goodsSpec = null;
        if(StringUtil.isEmptyString(attrId)) {
            goodsSpec = goodsSpecService.getDefaultGoodsSpec(id);
        } else {
            goodsSpec = goodsSpecService.getGoodsSpedByAttr(id, attrId);
        }
        List<GoodsAttrVo> attrList = getAttrVoInfo(model, id, goodsSpec);
        if(null != goodsSpec) {
            goodsInfo.setPrice(goodsSpec.getPrice());
            goodsInfo.setStock(goodsSpec.getStock());
            goodsInfo.setSpecificationId(goodsSpec.getId());
        }
        
        //查询当前商品的图片
        List<CommonImg> goodsImgList = commonImgService.selectImgsByObjIdAndObjType(id,OwnerConstants.IMG_GOODS);
        model.addAttribute("goodsImgList", goodsImgList);
        
        model.addAttribute("attrvoList", attrList);
		model.addAttribute("user", user);
    	model.addAttribute("goodsInfo", goodsInfo);
    	model.addAttribute("shopInfo", shopInfo);
    	
		model.addAttribute("type", goodsInfo.getType());
		
    	return "mall/goods_detail";
    }
    
    public List<GoodsAttrVo> getAttrVoInfo(Model model, String id, GoodsSpecification goodsSpec){
        List<GoodsAttrVo> voList = new ArrayList<GoodsAttrVo>();
        Map<String, String> defaultSpec = new HashMap<>();
        if(null != goodsSpec) {
            for(GoodsSpecificationAttr attr : goodsSpec.getGoodsSpecificationAttrs()) {
                defaultSpec.put(attr.getAttrValue(), attr.getAttrValue());
            }  
        }
        List<GoodsInfo> goodsInfos = goodsInfoService.getGoodsAttrById(id);
        GoodsAttrVo goodsAttrVo = null;
        
        Map<String, List<GoodsInfo>> map = new HashMap<>();
        for(GoodsInfo info : goodsInfos) {
            if(defaultSpec.containsKey(info.getAttrValue())) {
                info.setIsSelected(true);
            } else {
                info.setIsSelected(false);
            }
            if(map.containsKey(info.getAttrName())){
                map.get(info.getAttrName()).add(info);
            } else {
                List<GoodsInfo> tempList = new ArrayList<>();
                tempList.add(info);
                map.put(info.getAttrName(), tempList);
            }
        }
        
        for(String key : map.keySet()) {
            goodsAttrVo = new GoodsAttrVo();
            goodsAttrVo.setAttrName(key);
            goodsAttrVo.setAttrValues(map.get(key));
            
            voList.add(goodsAttrVo);
        }
    	return voList;
    }
    
    /**
     * @Description 返回积分商品兑换积分规则页面<br> 
     * @return
     */
    @RequestMapping(value = "integralRules", method = RequestMethod.GET)
    public String integralRules(Model model,HttpServletRequest req){
    	return "mall/integralgoods/integral_rules";
    	
    }
    
    /**
     * @Description 返回店铺首页<br>
     * 
     * @param model
     * @param pNo 当前页码
     * @param shopId 店铺id
     * @param firstCategoryId 商品一级分类id
     * @param goodsCategoryId 商品二级分类id
     * @param keyword 关键字搜索
     * @param shopName 店铺名称
     * @param req 用户请求
     * @return 返回店铺首页
     * @author caiyinli 13277030520@163.com
     * @date 2016年8月4日
     * @see 
     */
    @RequestMapping(value = "shopIndex", method = RequestMethod.GET)
    public String shopIndex(Model model,Integer pNo,String shopId, String firstCategoryId,String goodsCategoryId,String keyword,String shopName,HttpServletRequest req){
    	if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
    	   
        //根据店铺id查询店铺装修信息
        List<ShopBanner> shopBannerList = shopBannerService.selectBannerByShopId(shopId);
        model.addAttribute("shopBannerList", shopBannerList);
        model.addAttribute("bannerSize", shopBannerList.size());
    	
    	//取商品的一二级分类
    	List<GoodsCategory> goodsCategory  = goodsCategoryService.selectCategoryByShopId(shopId);
    	
    	//获取店铺优惠券
    	List<CouponInfo> couponInfos = couponInfoService.getCouponInfo(shopId);
    	
    	//存储二级分类的第一个分类id
    	String firstSecondId = "";
    	if (CollectionUtils.isNotEmpty(goodsCategory) && firstCategoryId == null) {
			model.addAttribute("secondCategory", goodsCategory.get(0).getSecondList());
			firstSecondId = goodsCategory.get(0).getSecondList().get(0).getId();
		}else if (CollectionUtils.isNotEmpty(goodsCategory) && firstCategoryId != null) {
			for(GoodsCategory itemCategory : goodsCategory){
				if (firstCategoryId.equals(itemCategory.getId())) {
					model.addAttribute("secondCategory", itemCategory.getSecondList());
					firstSecondId = itemCategory.getSecondList().get(0).getId();
				}
			}
		}
    	
    	ShopInfo shopInfo = shopInfoService.selectByPrimaryKey(shopId);
    	
    	List<GoodsInfo> goodsInfos = new ArrayList<GoodsInfo>();
    	//此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
    	if(goodsCategoryId == null){
    		goodsCategoryId = firstSecondId;
    	}
        PageHelper.startPage(pNo, Constants.PAGE_SIZE);
        goodsInfos = goodsInfoService.getByshopIdAndCategoryIdAndKeyword(shopId, goodsCategoryId, keyword);
        PageInfo<GoodsInfo> info = new PageInfo<GoodsInfo>(goodsInfos);
        
        
        model.addAttribute("goodsInfoList", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("shopName", shopName);
		model.addAttribute("shopId", shopId);
		model.addAttribute("keyword", keyword);
		model.addAttribute("firstCategoryId", firstCategoryId);
		model.addAttribute("goodsCategoryId", goodsCategoryId);
		model.addAttribute("goodsCategory", goodsCategory);
		model.addAttribute("couponInfos", couponInfos);
		model.addAttribute("shopInfo", shopInfo);
		
		
    	
    	return "mall/shop_index";
    	
    }
    
    /**
     * 
     * @Description 返回积分商品详情中的商品详情页面<br>
     * 
     * @param id 主键id
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月3日
     * @see
     */
    @RequestMapping(value = "integralDetail", method = RequestMethod.GET)
    public String integralDetail(Model model,String id,HttpServletRequest req){
    	GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(id);
    	ShopInfo shopInfo = shopInfoService.selectByPrimaryKey(goodsInfo.getShopId());
    	
    	model.addAttribute("goodsInfo", goodsInfo);
    	model.addAttribute("shopInfo", shopInfo);
    	
    	return "mall/integralgoods/integral_goods_view_detail";
    	
    }
    
    /**
     * @Description 返回积分商品详情中的商品规格参数页面<br> 
     * @param id 主键id
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月3日
     * @see
     */
    @RequestMapping(value = "showIntegralParam", method = RequestMethod.GET)
    public String showIntegralParam(Model model,String id,HttpServletRequest req){
    	GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(id);
    	ShopInfo shopInfo = shopInfoService.selectByPrimaryKey(goodsInfo.getShopId());
    	
    	model.addAttribute("goodsInfo", goodsInfo);
    	model.addAttribute("shopInfo", shopInfo);
    	
    	return "mall/integralgoods/integral_goods_params";
    	
    }
    
    /**
     * @Description 返回积分商品详情中的商品评论页面<br> 
     * @param id 主键id
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月3日
     * @see
     */
    @RequestMapping(value = "integralGoodsComments", method = RequestMethod.GET)
    public String integralGoodsComments(Model model,String id,HttpServletRequest req){
    	GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(id);
    	ShopInfo shopInfo = shopInfoService.selectByPrimaryKey(goodsInfo.getShopId());
    	List<GoodsComment> goodsComments = goodsCommentService.selectCommentByGoodsId(id);
    	
    	model.addAttribute("goodsInfo", goodsInfo);
    	model.addAttribute("shopInfo", shopInfo);
    	model.addAttribute("goodsComments", goodsComments);
    	
    	return "mall/integralgoods/integral_goods_comments";
    	
    }
    
    /**
     * @Description 返回商品或店铺搜索结果页面<br>
     * @param pNo 当前页码
     * @param selectType 1为商品，2为店铺
     * 
     * @param name 商品名或店铺名 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月3日
     */
    @RequestMapping(value = "searchGoodsOrShops", method = RequestMethod.GET)
    public String searchGoodsOrShops(Model model,Integer pNo,Integer order, Integer selectType, String name, HttpServletRequest req){
    	
    	String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
    	if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
    	//此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, 15);
        if(StringUtils.isNotEmpty(name)){
        	model.addAttribute("name", name);	
        }
        //选择商品，搜索商品
        if(Constants.GOODS_SEARCH == selectType){
        	
        	if(order != null ){
        		List<GoodsInfo> goodsInfos = goodsInfoService.getmallGoodsByFilter(order, cellId);
        		PageInfo<GoodsInfo> info = new PageInfo<GoodsInfo>(goodsInfos);
        		List<GoodsCategory> goodsCategories = goodsInfoService.getGoodsCategorysList();
        		
        		model.addAttribute("goodsInfos", info);
        		model.addAttribute("goodsCategories", goodsCategories);
        	} else {
        		List<GoodsInfo> goodsInfos = goodsInfoService.getGoodsInfoByName(name, cellId);
                PageInfo<GoodsInfo> info = new PageInfo<GoodsInfo>(goodsInfos);
                List<GoodsCategory> goodsCategories = goodsInfoService.getGoodsCategorysList();
        		
        		model.addAttribute("goodsInfos", info);
        		model.addAttribute("goodsCategories", goodsCategories);
        	}
        
        }
        //选择店铺，则搜索店铺
        if(Constants.SHOP_SEARCH == selectType){
        List<ShopInfo> shopInfos = shopInfoService.getShopInfoByName(name, cellId);	
        PageInfo<ShopInfo> infos = new PageInfo<ShopInfo>(shopInfos);
        
        model.addAttribute("shopInfos", infos);
        }
        
        model.addAttribute("selectType", selectType);
        model.addAttribute("currentPage", pNo);
        model.addAttribute("order", order);
        
    	return "mall/goodsShop_search_results";
    	
    } 
    
    /**
     * 
     * @Description 返回商品搜索结果页面<br>
     * 
     * @param pNo 当前页
     * @param order 1：销量从低到高 2：销量从高到低 3：价格从低到高  4：价格从高到低 5：评论从少到多  
     *              6：评论从多到少 7：上架时间从以前到最新  8：上架时间从最新到以前
     * @param selectType  1为商品，2为店铺
     * @return 
     * @see
     */
    @RequestMapping(value = "mallGoods", method = RequestMethod.GET)
    public String mallGoods(Model model,Integer pNo, Integer order, Integer selectType,HttpServletRequest req){
    	
    	String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
    	if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
    	//此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, 15);
        
    	if(null == selectType){
    		selectType = Constants.GOODS_SEARCH;
    	}
        //选择商品，搜索商品
        List<GoodsInfo> goodsInfos = goodsInfoService.getmallGoodsByFilter(order, cellId);
        PageInfo<GoodsInfo> info = new PageInfo<GoodsInfo>(goodsInfos);
        List<GoodsCategory> goodsCategories = goodsInfoService.getGoodsCategorysList();
        
        model.addAttribute("goodsCategories", goodsCategories);
        
        model.addAttribute("goodsInfos", info);
        model.addAttribute("order", order);
        model.addAttribute("selectType", selectType);
        model.addAttribute("currentPage", pNo);
        
    	return "mall/goodsShop_search_results";
    	
    } 
    
    /**
     * 
     * @Description 根据商品分类搜索商品<br>
     * 
     * @param pNo 当前页
     * @param goodCategoryId 商品分类id
     * @param order 1：销量从低到高 2：销量从高到低 3：价格从低到高  4：价格从高到低 5：评论从少到多 
     *              6：评论从多到少 7：上架时间从以前到最新  8：上架时间从最新到以前
     * @param selectType  1为商品，2为店铺
     * @return 
     * @author lilan 1063972142@qq.com
     * @see
     */
    @RequestMapping(value = "searchGoodsByCategoryId", method = RequestMethod.GET)
    public String searchGoodsByCategoryId(Model model,Integer pNo, String goodCategoryId, Integer order,Integer selectType,HttpServletRequest req){
    	
    	String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
    	if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
    	//此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, PAGE_SIZE);
        
    	if(null == selectType){
    		selectType = Constants.GOODS_SEARCH;
    	}
    	
    	if(null == order){
    		order = Constants.GOODS_SEARCH;
    	}
    	
        List<GoodsInfo> goodsInfos = goodsInfoService.searchGoodsByCategoryId(goodCategoryId, order, cellId);
        PageInfo<GoodsInfo> info = new PageInfo<GoodsInfo>(goodsInfos);
        List<GoodsCategory> goodsCategories = goodsInfoService.getGoodsCategorysList();
        
        model.addAttribute("goodsCategories", goodsCategories);
        model.addAttribute("goodsInfos", info);
        model.addAttribute("order", order);
        model.addAttribute("selectType", selectType);
        model.addAttribute("goodCategoryId", goodCategoryId);
        model.addAttribute("currentPage", pNo);
        
    	return "mall/goods_search_results";
    	
    } 
    
    
}
